package com.milestone.fitnesspersonaltrainer.view.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.milestone.fitnesspersonaltrainer.R;
import com.milestone.fitnesspersonaltrainer.adapter.DietAdapter;
import com.milestone.fitnesspersonaltrainer.base.BaseFragment;
import com.milestone.fitnesspersonaltrainer.constants.ConstantCode;
import com.milestone.fitnesspersonaltrainer.databinding.FragmentDietBinding;
import com.milestone.fitnesspersonaltrainer.listener.OnRecyclerViewItemClickListener;
import com.milestone.fitnesspersonaltrainer.utils.GlideImageLoader;
import com.milestone.fitnesspersonaltrainer.vewmodel.DietViewModel;
import com.milestone.fitnesspersonaltrainer.view.activity.DetailsActivity;
import com.milestone.fitnesspersonaltrainer.view.activity.DietListActivity;

import java.util.ArrayList;
import java.util.List;

public class DietFragment extends BaseFragment<FragmentDietBinding, DietViewModel> {

    private DietAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_diet;
    }

    @Override
    protected int getVeriableId() {
        return 0;
    }

    @Override
    protected DietViewModel createFragmentViewModel() {
        return new DietViewModel(this);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initPara();
        setListener();
    }

    private void initPara() {
        List<String> imgs = new ArrayList<>();
        imgs.add(ConstantCode.TEST_IMG_URL);

        binding.banner.setDelayTime(3000)
                .setImages(imgs)
                .setImageLoader(new GlideImageLoader())
                .start();

        setDrawTop(binding.breakfastIcon, R.drawable.diet_icon_breakfast);
        setDrawTop(binding.lunchIcon, R.drawable.diet_icon_lunch);
        setDrawTop(binding.dinnerIcon, R.drawable.diet_icon_dinner);

        adapter = new DietAdapter(getContext());
        binding.dietRecycler.setLayoutManager(new GridLayoutManager(getContext(), 2));
        binding.dietRecycler.setAdapter(adapter);

        adapter.getItems().add("1");
        adapter.getItems().add("1");
        adapter.getItems().add("1");
        adapter.getItems().add("1");
        adapter.getItems().add("1");
    }

    private void setListener() {
        binding.breakfast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), DietListActivity.class));
            }
        });
        binding.lunch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), DietListActivity.class));
            }
        });
        binding.dinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), DietListActivity.class));
            }
        });
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getContext(), DetailsActivity.class));
            }
        });
    }

    private void setDrawTop(TextView textView, int res){
        Drawable drawable = getResources().getDrawable(res);
        drawable.setBounds(0,0,80,80);
        textView.setCompoundDrawables(null,drawable,null,null);
    }
}
